﻿package {
	import flash.display.*;
	import flash.text.*;
	import flash.events.*;
	import flash.net.*;
	import flash.filters.*;
	import fl.transitions.*;
	import fl.transitions.easing.*;
	import fl.controls.*;
	import src.cenfun.*;
	public class Main extends Sprite {
		private var bw:Number = 400;
		private var bh:Number = 300;
		private var border:Sprite = new Sprite();
		private var bodyMask:Sprite = new Sprite();
		private var body:Sprite = new Sprite();
		private var img:Sprite = new Sprite();
		private var imgBody:Sprite = new Sprite();
		private var btRotation:Button = new Button();
		private var btZoom1:Button = new Button();
		private var btZoom0:Button = new Button();
		public function Main() {
			bodyMask.graphics.beginFill(0xffffff);
			bodyMask.graphics.drawRect(0, 0, bw, bh);
			bodyMask.graphics.endFill();
			bodyMask.x = (stage.stageWidth - bw)/2;
			bodyMask.y = (stage.stageHeight - bh)/2;
			addChild(bodyMask);
			body.x = (stage.stageWidth - bw)/2;
			body.y = (stage.stageHeight - bh)/2;
			body.mask = bodyMask;
			addChild(body);
			//
			var w:int = 2;
			border.graphics.lineStyle(w, 0xcccccc);
			border.graphics.drawRect(0, 0, bw+2*w, bh+2*w);
			border.x = (stage.stageWidth - bw)/2-w;
			border.y = (stage.stageHeight - bh)/2-w;
			addChild(border);
			//
			btRotation.label = "Rotate";
			btRotation.addEventListener(MouseEvent.MOUSE_DOWN, imgRotation);
			addChild(btRotation);
			btZoom1.label = "Zoom+";
			btZoom1.addEventListener(MouseEvent.MOUSE_DOWN, imgZoom1);
			btZoom1.x = 100;
			addChild(btZoom1);
			btZoom0.label = "Zoom-";
			btZoom0.addEventListener(MouseEvent.MOUSE_DOWN, imgZoom0);
			btZoom0.x = 200;
			addChild(btZoom0);
			loadImg();
		}
		private function loadImg():void {
			var loader:Loader = new Loader();
			loader.load(new URLRequest("img.jpg"));
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE,setImg);
		}
		private function setImg(e:Event):void {
			imgBody.addChild(e.target.content);
			imgBody.x = -imgBody.width/2;
			imgBody.y = -imgBody.height/2;
			imgBody.addEventListener(MouseEvent.MOUSE_DOWN, sdrag);
			img.addChild(imgBody);
			initSize();
			//
			body.addChild(img);
		}
		private function initSize():void {
			var tw:Number = img.width;
			var th:Number = img.height;
			var twh:Number = tw/th;
			var wh:Number = bw/bh;
			if (wh<twh) {
				img.width = bw;
				img.height = bw/twh;
			} else {
				img.width = bh*twh;
				img.height = bh;
			}
			img.x = bw/2;
			img.y = bh/2;
		}
		private function sdrag(e:MouseEvent):void {
			imgBody.startDrag();
			stage.addEventListener(MouseEvent.MOUSE_UP, edrag);
		}
		private function edrag(e:MouseEvent):void {
			imgBody.stopDrag();
			stage.removeEventListener(MouseEvent.MOUSE_UP, edrag);
		}
		//
		private function imgRotation(e:Event):void {
			stage.addEventListener(MouseEvent.MOUSE_UP, imgRotationStop);
			stage.addEventListener(Event.ENTER_FRAME, rotateHandler);
		}
		private function imgRotationStop(e:Event):void {
			stage.removeEventListener(MouseEvent.MOUSE_UP, imgRotationStop);
			stage.removeEventListener(Event.ENTER_FRAME, rotateHandler);
		}
		private function rotateHandler(e:Event):void {
			img.rotation += 1;
		}
		//
		private function imgZoom1(e:Event):void {
			stage.addEventListener(MouseEvent.MOUSE_UP, imgZoom1Stop);
			stage.addEventListener(Event.ENTER_FRAME, zoom1Handler);
		}
		private function imgZoom1Stop(e:Event):void {
			stage.removeEventListener(MouseEvent.MOUSE_UP, imgZoom1Stop);
			stage.removeEventListener(Event.ENTER_FRAME, zoom1Handler);
		}
		private function zoom1Handler(e:Event):void {
			var scale:Number = img.scaleX*1.01;
			if (scale>0) {
				img.scaleX = scale;
				img.scaleY = scale;
			}
		}
		private function imgZoom0(e:Event):void {
			stage.addEventListener(MouseEvent.MOUSE_UP, imgZoom0Stop);
			stage.addEventListener(Event.ENTER_FRAME, zoom0Handler);
		}
		private function imgZoom0Stop(e:Event):void {
			stage.removeEventListener(MouseEvent.MOUSE_UP, imgZoom0Stop);
			stage.removeEventListener(Event.ENTER_FRAME, zoom0Handler);
		}
		private function zoom0Handler(e:Event):void {
			var scale:Number = img.scaleX*0.99;
			if (scale>0) {
				img.scaleX = scale;
				img.scaleY = scale;
			}
		}
		//
		private function getShadow(c:Number,a:Number):Array {
			return new Array(new DropShadowFilter(1,45,c,a,4,4,1,1));
		}
	}

}